package cn.ekuma.epos.datalogic.define.dao.erp;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_SharedLinkOrder;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.SharedLinkOrder;
import com.openbravo.bean.erp.SharedOrder;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class SharedLinkOrderDAO extends ModifiedLogDAO<SharedLinkOrder>{

    public SharedLinkOrderDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                  "SHARED_LINK_ORDERS",
                  new Field[]{
                       new Field(I_SharedLinkOrder.ID,Datas.STRING,Formats.STRING),
                       new Field("CONTENT",Datas.BYTES,Formats.BYTEA),
                       new Field("LOCKED",Datas.BOOLEAN,Formats.BOOLEAN),
                       new Field("DEFINETYPEREF",Datas.INT,Formats.INT),
                       new Field("ORDERTYPE",Datas.INT,Formats.INT),
                       
                       new Field("ORDERID",Datas.STRING,Formats.STRING),
                       new Field("CURDATE",Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field("PERSON",Datas.STRING,Formats.STRING),
                       new Field("CUSTOMER",Datas.STRING,Formats.STRING),
                       new Field("PRICERATE",Datas.DOUBLE,Formats.DOUBLE),
                       
                       new Field("ATTRIBUTESETINSTANCE_ID",Datas.STRING,Formats.STRING),
                       new Field("TOTALPRICE",Datas.DOUBLE,Formats.DOUBLE),
                       new Field("LOCATIONREF",Datas.STRING,Formats.STRING),
                       new Field("LOCATIONREF1",Datas.STRING,Formats.STRING),
                       new Field("MEMO",Datas.STRING,Formats.STRING),
                       
                       new Field("REFERENCEPERSON",Datas.STRING,Formats.STRING),
                       new Field("STATE",Datas.INT,Formats.INT),
                       new Field("LASTMODIFIED",Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field("LOCKUSERID",Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field("REFDATE",Datas.TIMESTAMP,Formats.TIMESTAMP)
                      },
                  new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, SharedLinkOrder obj) throws BasicException {
    	Order order=obj.getOrder();
    	dp.setString(1, obj.getId());
    	dp.setBytes(2, ImageUtils.writeSerializable(order));
    	dp.setBoolean(3, obj.isLocked());
    	dp.setString(4, order.getDefineType().getId());
    	dp.setInt(5, order.getOrderType());
    	
    	dp.setString(6, order.getOrderId());
    	dp.setTimestamp(7, order.getDate());
    	dp.setString(8, order.getUser().getId());
    	dp.setString(9,order.getCustomer()==null?null:order.getCustomer().getId());
    	dp.setDouble(10, order.getM_dPriceRate());
    	
    	dp.setString(11, order.getAttsetinstid());
    	dp.setDouble(12, order.getTotalPrice());
    	dp.setString(13, order.getLocationRef());
    	dp.setString(14, order.getLocationRef1());
    	dp.setString(15, order.getMemo());
    	
         if(order.getReference_User()!=null)
        	 dp.setString(16,order.getReference_User().getId());
        else
        	dp.setString(16,null);
        dp.setString(17,order.getCurrentState());
        dp.setTimestamp(18, obj.getLastModified());
        dp.setString(19,obj.getLockUserId());
        dp.setTimestamp(20,order.getM_dRefDate());
    }

    public SharedLinkOrder readValues(DataRead dr) throws BasicException {
       throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Class getSuportClass() {
        return SharedOrder.class;
    }
    
    
    public Order getSharedOrder(String Id) throws BasicException {
        if (Id == null) {
            return null;
        } else {
            Object[]record = (Object[]) new StaticSentence(s
                    , "SELECT CONTENT FROM SHARED_LINK_ORDERS WHERE ID = ?"
                    , SerializerWriteString.INSTANCE
                    , new SerializerReadBasic(new Datas[] {Datas.SERIALIZABLE})).find(Id);
            return record == null ? null : (Order) record[0];
        }
    }

	@Override
	public SharedLinkOrder readValues(DataRead dr, SharedLinkOrder obj)
			throws BasicException {
		// TODO Auto-generated method stub
		return null;
	}
}
